package com.wocao.utils.log;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wocao.entity.ResponseResult;
import com.wocao.entity.SysDictType;
import com.wocao.entity.SysOperLog;
import com.wocao.service.SysOperLogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Map;


/**
 * 日志通知类
 */
// 1. 配置增强  被扫描到自动加到容器中去
@Aspect
// 1. 交给IOC管理
@Component
@EnableAspectJAutoProxy
public class OperLogAdvise {

    @Autowired
    private SysOperLogService sysOperLogService;

    // 2.
    // 定义切点
//    @Pointcut("execution(* com.wocao.controller.*Controller.*(..)) && @annotation(com.wocao.utils.log.OperLog)")
//    public void pointcut() {
//    }

    @Around("@annotation(operLog)")
    public Object around(ProceedingJoinPoint joinPoint,OperLog operLog) throws Throwable {
        Object proceed = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        // 获取所有前置需要的值
        SysOperLog sysOperLog = new SysOperLog();
        //业务类型（0其它 1新增 2修改 3删除 4导入 5导出 6授权 7强退 8生成代码 9清除缓存）
        sysOperLog.setOperatorType(Long.parseLong(operLog.decideBusinessType()));
        /*获取方法名*/
        sysOperLog.setMethod(joinPoint.getSignature().getName());
        /*请求地址*/
        sysOperLog.setRequestMethod(request.getMethod());
        /*执行时间*/
        sysOperLog.setOperTime((LocalDateTime.now()));
        Object[] args = joinPoint.getArgs();
        /*请求参数*/
        Map<String, String[]> paramMap = request.getParameterMap();
        String reqParam = JSON.toJSONString(paramMap);
        sysOperLog.setOperParam(reqParam);
        /*获取请求URL*/
        sysOperLog.setOperUrl(request.getRequestURL().toString());
        /*获取主机ip地址——地址描述*/
        String meId = request.getRemoteAddr();
        sysOperLog.setOperIp(meId);
        if (meId.equals("127.0.0.1")) {
            sysOperLog.setOperLocation("内网IP");
        } else {
            sysOperLog.setOperLocation("XX XX");
        }
        /*响应参数*/

        // 执行原有方法
        try {
            proceed = joinPoint.proceed();
            /*执行后期通知*/
            ResponseResult<Page<SysDictType>> responseResult = (ResponseResult<Page<SysDictType>>) proceed;
            sysOperLog.setJsonResult(responseResult.toString());
            sysOperLogService.save(sysOperLog);
            return proceed;
        } catch (Exception e) {
            e.printStackTrace();
            /*获取异常信息*/
//            if (responseResult.getMessage() != null){
//                sysOperLog.setErrorMsg(responseResult.getMessage());
//            }
            sysOperLog.setJsonResult(null);
            System.out.println(e.getMessage());
        }
        return proceed;
    }
}
